package com.ssi.mrms.action.room;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.ssi.mrms.action.BaseAction;
import com.ssi.mrms.model.Msg;
import com.ssi.mrms.model.Msg.MsgType;
import com.ssi.mrms.model.RMaccrModel;
import com.ssi.mrms.model.TowerModel;
import com.ssi.mrms.service.CarCardService;
import com.ssi.mrms.service.ComboboxService;
import com.ssi.mrms.utils.DateUtils;
import com.ssi.mrms.view.SelectBoxView;


@Namespaces(value = { @Namespace("/room") })
public class RM001AAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(RM001AAction.class);
    private ComboboxService comboboxService  = lookupBean(ComboboxService.class, "ComboboxService");

    
    private List<SelectBoxView> comboTower = null;
    private List<SelectBoxView> comboFloor = null;
    private List<SelectBoxView> comboUnit = null;
    private List<SelectBoxView> comboAddress = null;
    
    private RMaccrModel model;
    
    private String mode = "Add";
    
    private String accrcredat ;
    private String accrupddat ;
    private String accrstatdat ;
    private String accratvdat ;
    
    private Integer cardId;
    
    private CarCardService carCardService = lookupBean(CarCardService.class, "CarCardService");
    
	private TowerModel towerModel;
	private String tower;
	private String floor;
	private String unit;
	private String addr;
    
    public RM001AAction() {
    	logger.info("## ");
		comboTower = comboboxService.getTower();
		comboFloor = comboboxService.getFloor(null);
		comboUnit = comboboxService.getUnit(null);
		comboAddress = comboboxService.getAddress(null);
//		model = new RMaccrModel();
	}
    
	@Action(value = "/RM001A", results = { @Result(name = "success", location = "RM001A.tiles", type = "tiles") })
	public String init() throws Exception {
		logger.info("## init"+this.getClass().getSimpleName());
	    mode = "add";
		model = new RMaccrModel();

		return SUCCESS;
	}
	
	@Action(value = "/RM001Aadd", results = { @Result(name = "success", location="RM001", type = "redirect" , params = {"msg.msg", "S001"}) })
	public String save() throws Exception {
		Msg m = new Msg();
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("RM001Aadd delete >>>> model: "+ToStringBuilder.reflectionToString(model));
//		Date cd = DateUtils.getDateFormate(accrcredat);
//		Date ud = DateUtils.getDateFormate(accrupddat);
		
//		model.setAccrcredat(cd);
//		model.setAccrupddat(ud);
		
//		towerModel = new TowerModel();
//		towerModel.setTower(tower);
//		towerModel.setFloor(floor);
//		towerModel.setUnitNo(unit);
//		towerModel.setAddress(addr);
		try{
		if(mode.equalsIgnoreCase("add")){
			
//			TowerModel re = carCardService.findRoomId(towerModel);
//			if(re != null){
//				model.setRlrmid(re.getRoomId());
				carCardService.save(model);
//				logger.info(SUCCESS);
//				m.setType(MsgType.SUCCESS );
//				m.setMsg(SUCCESS);
//			}else{
//				logger.info("######## roomID not found");
//				m.setType(MsgType.ERROR );
//				m.setMsg("Room  not found");
				
//			}
			
		}else{
			// edit
//			RMaccrModel c = carCardService.getCarCardById(model.getAccrid());
//			model.setRlrmid(c.getRlrmid());
			carCardService.update(model);
//			m.setType(MsgType.SUCCESS );
//			m.setMsg(SUCCESS);
		}
		
		}catch( Exception e){
			logger.info(" ERROR ID : " + e.getMessage());
			e.printStackTrace();
			m.setType(MsgType.ERROR );
			m.setMsg(e.getMessage()	);
			setMsg(m);
		}
		setMsg(m);
		return SUCCESS;
	}
	@Action(value = "/RM001Aedit", results = { @Result(name = "success", location = "RM001A.tiles", type = "tiles" , params = {"msg.msg", "D001"}) })
	public String addEdit() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
	    mode = "edit";

	    RMaccrModel c = carCardService.getCarCardById(cardId);
		if( c != null){
			accrcredat = DateUtils.getDateStringFormate(c.getAccrcredat());
			accrupddat = DateUtils.getDateStringFormate(c.getAccrupddat());
			accrstatdat = DateUtils.getDateStringFormate(c.getAccrstatdat());
			accratvdat = DateUtils.getDateStringFormate(c.getAccratvdat());
		}
		
		// show
		this.setModel(c);
		
		return SUCCESS;
	}
	@Action(value = "/RM001ADelete", results = { @Result(name = "success", location = "/room/RM001", type = "redirect") })
	public String delete() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		
		RMaccrModel c = carCardService.getCarCardById(cardId);
		if( c != null){
			carCardService.delete(c);
		}else{
			logger.info("error not found !! " + cardId);
		}
		Msg  m = new Msg();
			m.setType(MsgType.SUCCESS);
			m.setMsg(SUCCESS);
		return SUCCESS;
	}
	
	
	
	public List<SelectBoxView> getComboTower() {
		return comboTower;
	}
	public void setComboTower(List<SelectBoxView> comboTower) {
		this.comboTower = comboTower;
	}
	public List<SelectBoxView> getComboFloor() {
		return comboFloor;
	}
	public void setComboFloor(List<SelectBoxView> comboFloor) {
		this.comboFloor = comboFloor;
	}
	public List<SelectBoxView> getComboUnit() {
		return comboUnit;
	}
	public void setComboUnit(List<SelectBoxView> comboUnit) {
		this.comboUnit = comboUnit;
	}
	public List<SelectBoxView> getComboAddress() {
		return comboAddress;
	}
	public void setComboAddress(List<SelectBoxView> comboAddress) {
		this.comboAddress = comboAddress;
	}

	public RMaccrModel getModel() {
		return model;
	}

	public void setModel(RMaccrModel model) {
		this.model = model;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getAccrcredat() {
		return accrcredat;
	}

	public void setAccrcredat(String accrcredat) {
		this.accrcredat = accrcredat;
	}

	public String getAccrupddat() {
		return accrupddat;
	}

	public void setAccrupddat(String accrupddat) {
		this.accrupddat = accrupddat;
	}

	public Integer getCardId() {
		return cardId;
	}

	public void setCardId(Integer cardId) {
		this.cardId = cardId;
	}

	public ComboboxService getComboboxService() {
		return comboboxService;
	}

	public void setComboboxService(ComboboxService comboboxService) {
		this.comboboxService = comboboxService;
	}

	public TowerModel getTowerModel() {
		return towerModel;
	}

	public void setTowerModel(TowerModel towerModel) {
		this.towerModel = towerModel;
	}

	public String getTower() {
		return tower;
	}

	public void setTower(String tower) {
		this.tower = tower;
	}

	public String getFloor() {
		return floor;
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getAccratvdat() {
		return accratvdat;
	}

	public void setAccratvdat(String accratvdat) {
		this.accratvdat = accratvdat;
	}

	public String getAccrstatdat() {
		return accrstatdat;
	}

	public void setAccrstatdat(String accrstatdat) {
		this.accrstatdat = accrstatdat;
	}

	
}
